<?php
namespace app\index\controller;
use app\index\model\Cate;
use app\index\model\Collect;
use app\index\model\Good;
use app\index\model\Question;
use app\index\model\Reply;
use think\Controller;
use think\Db;
use think\Loader;
class Jie extends Common
{

	public function index()
	{
		$id = input('id/d');

        // $id = intval($id);

        var_dump($id);

        // $sql = "select * from fly_qestion where id = ".$id;

        // var_dump($sql);exit;

		Db::name('question')->where('question_id', $id)->setInc('view_num');

        $uid  = session('user_id') ? session('user_id') :'null';

		$data = Db::name('question')
		->alias('t1')
		->field('t1.*,t2.nickname,t2.face,t3.cate_name,t4.collect_id')
		->join('user t2','t1.user_id = t2.user_id')
        ->join('cate t3','t1.cate_id = t3.cate_id')
		->join('collect t4','t1.question_id = t4.question_id and t4.user_id='.$uid,'left')
		->where('t1.question_id',$id)
		->find();

        // $collect = Collect::get(['question_id'=>$id,'user_id'=>session('user_id')]);

        // var_dump($collect);


        $replyData = Db::name('reply')
        ->alias('t1')
        ->field('t1.*,t2.nickname,t2.face,t3.good_id')
        ->join('user t2','t1.user_id = t2.user_id')
        ->join('good t3','t1.reply_id = t3.reply_id and t3.user_id='.$uid,'left')
        ->where('t1.question_id',$id)
        ->order('t1.create_time desc')
        ->select();

		if($data && $data['ext_info']){
			$data['ext_info'] = json_decode($data['ext_info'],true);
		}

		$hotReply = Question::order('reply_num desc')->limit(10)->select();

        $this->assign('title',$data['title']);
        $this->assign('data',$data);
		$this->assign('replyData',$replyData);
		$this->assign('hotReply',$hotReply);
		return $this->fetch();
	}
    public function add()
    {

    	$cates = Cate::all();
    	$this->assign('cates',$cates);
        return $this->fetch();
    }

    public function post()
    {
    	$postData = input('post.');


        $validate = Loader::validate('Question');

        if(!$validate->check($postData)){
            $this->error($validate->getError());
        }

        if($postData['cate_id']==4){
        	$postData['ext_info'] = json_encode($postData['ext_info']);
        }else{
        	unset($postData['ext_info']);
        }

        $postData['user_id'] = session('user_id');

        $res = Question::create($postData);


        Db::name('user')->where('user_id', session('user_id'))->setDec('kiss', $postData['kiss']);


        if($res){
            $this->success('发布成功',url('index/jie/index',['id'=>$res->question_id]));
        }else{
            $this->error('发布失败');
        }
    }

    public function upload()
    {
    	 // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');

            if($info){
              	$name = $info->getFilename();

                $filePath = "/uploads/".date("Ymd").'/'.$name;

                if($filePath){
                    $this->error('上传成功','',['src'=>$filePath]);
                }else{
                    $this->success('上传失败');
                }

            }else{
                // 上传失败获取错误信息
                $this->success($file->getError());
            }
        }
    }

    public function reply()
    {
        $postData = input('post.');

        if(strlen($postData['content'])<10){
            $this->error('内容长度不够');
        }

        if(!isset($postData['question_id'])){
            $this->error('问题id必须有');
        }

        $postData['user_id'] = session("user_id");


        $res = Reply::create($postData);

        Db::name('question')->where('question_id',$postData['question_id'])->setInc('reply_num');

        if($res){
            $this->success('发布成功');
        }else{
            $this->error('发布失败');
        }
    }

    public function collect()
    {
        $question_id = input('question_id');

        $collect = Collect::get(['question_id'=>$question_id,'user_id'=>session('user_id')]);

        if($collect){

            $res = $collect->delete();

            if($res){
                $this->success('取消收藏');
            }else{
                $this->error('收藏失败');
            }

        }else{

            $res = Collect::create(['question_id'=>$question_id,'user_id'=>session('user_id')]);

            if($res){
                $this->success('收藏成功');
            }else{
                $this->error('收藏失败');
            }

        }
    }

    public function zan()
    {
        $reply_id = input('reply_id');

        $good = Good::get(['reply_id'=>$reply_id,'user_id'=>session('user_id')]);

        if($good){

            $res = $good->delete();

            Db::name('reply')->where('reply_id', $reply_id)->setDec('zan_num');

            if($res){
                $this->success('取消点赞');
            }else{
                $this->error('点赞失败');
            }

        }else{

            $res = good::create(['reply_id'=>$reply_id,'user_id'=>session('user_id')]);

           Db::name('reply')->where('reply_id', $reply_id)->setInc('zan_num');


            if($res){
                $this->success('点赞成功');
            }else{
                $this->error('点赞失败');
            }

        }
    }
}
